#include "stdafx.h"
#include "RuntimeInfo.h"

FileVersion::FileVersion(LPCTSTR lpszFilePath )
{
	m_FilePath=lpszFilePath;
	m_dwLastError = FileVersion::GetFileVersion(lpszFilePath,&m_VersionInfo);
	if( m_dwLastError == ERROR_SUCCESS){
		m_dwMajorVersion = m_VersionInfo.dwFileVersionMS >> 16;
		m_dwMinorVersion = m_VersionInfo.dwFileVersionMS & 0xffff;
		m_dwReleaseNumber = m_VersionInfo.dwFileVersionLS >> 16 ;
		m_dwBuildNumber   = m_VersionInfo.dwFileVersionLS & 0xffff ;
	}
}
VOID FileVersion::CheckVersionInfo()
{
	static TCHAR chBuffer[256];
	if( m_dwLastError != ERROR_SUCCESS){
		sprintf_s(chBuffer ,256,"Get version info faild ,error code %d\n",m_dwLastError);
		throw chBuffer;
	}
}
DWORD FileVersion::GetMajorVersion()
{
	CheckVersionInfo();
	return m_dwMajorVersion;

}
DWORD FileVersion::GetMinorVersion()
{
	CheckVersionInfo();
	return m_dwMinorVersion;
}
DWORD FileVersion::GetReleaseNumber()
{
	CheckVersionInfo();
	return m_dwReleaseNumber;
}
DWORD FileVersion::GetBuildNumber()
{
	CheckVersionInfo();
	return m_dwBuildNumber;
}
DWORD FileVersion::GetFileVersion(LPCTSTR lpszFilePath,VS_FIXEDFILEINFO *pvsf )
{
	DWORD dwLastError;
	BOOL bFailed=TRUE;
	DWORD dwHandle;
	DWORD dwSize = GetFileVersionInfoSize(lpszFilePath,&dwHandle);
	if( dwSize > 0 ){
		char* pVer = new char[dwSize];
		BOOL bRet = GetFileVersionInfo(lpszFilePath,dwHandle,dwSize,pVer);
		if( bRet ){
			UINT uLen ;
			PVOID pBuf;
			bRet = VerQueryValue(pVer,"\\",&pBuf,&uLen);
			if( bRet ){
				memcpy(pvsf,pBuf,sizeof(VS_FIXEDFILEINFO));
				bFailed =FALSE;
			}
		}
		delete pVer;
	}
	if( bFailed )
		dwLastError=GetLastError();
	else
		dwLastError = ERROR_SUCCESS;
	return dwLastError;

}

RuntimeInfo::RuntimeInfo(LPCTSTR lpszFilePath,LPCTSTR lpszRegistryPath):m_IEVersion(lpszFilePath)
{
	m_RegistryPath = lpszRegistryPath;
}


RuntimeInfo::~RuntimeInfo(void)
{
}

